<template>
  <div class="control-item">
    <div class="control-header">
      <span class="control-name">点餐模板</span>
    </div>
    <div class="control-body">
      <div class="control-section">
        <!-- 模板样式 -->
        <div class="control-item">
          <div class="control-title">模板样式</div>
          <div class="control-content">
            <div class="example-list">
              <div
                class="example-item"
                v-for="(item, index) in exampleList"
                @click="setStyleType(item)"
                :class="styleType == item.value ? 'active' : ''"
                :key="index"
              >
                <img :src="item.icon" alt="" />
              </div>
            </div>
          </div>
        </div>
        <!-- 商品样式 -->
        <div class="control-item">
          <div class="control-title">模板样式</div>
          <div class="control-content">
            <div class="goods-example-list">
              <div
                class="example-item"
                v-for="(item, index) in goodsExampleList"
                @click="setGoodsStyleType(item)"
                :class="goodsType == item.value ? 'active' : ''"
                :key="index"
              >
                <img :src="item.icon" alt="" />
              </div>
            </div>
          </div>
        </div>
        <!-- 左侧导航背景 -->
        <!-- <colorControl
          :color.sync="params.params.leftTabBg"
          title="左侧导航背景"
          @reset="resetItem('leftTabBg')"
          @change="setLeftTabBg"
        ></colorControl> -->

        <!-- 字体配置 -->
        <fontControl
          :fontData.sync="params.params.fontCss"
          @resetFontCss="resetFontCss"
          :showAlign="false"
        ></fontControl>
        <!-- 店铺logo设置 -->
        <uploadImg
          :uploadImg="logoImg"
          size="96*96px"
          title="店铺logo设置"
          :noReset="true"
          @uploadSuccess="logoSuccess"
          @deleteImg="deleteLogo"
        ></uploadImg>
        <!-- 轮播图片设置 -->
        <uploadImg
          type="list"
          title="轮播图设置"
          :imgList="bannerList"
          :propActiveIndex="currentIndex"
          :noReset="true"
          @uploadSuccess="uploadSuccess"
          @deleteImg="deleteImg"
          @changeImg="changeImg"
          @handleImgItem="handleImgItem"
        ></uploadImg>
        <!-- 选择链接 -->
        <div class="choose-link" v-if="showChooseLink && bannerList.length > 0">
          <chooseLink :link="currentItem" @chooseEnd="chooseLink"></chooseLink>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import classifyDefault from "@/assets/images/diy/product/classift_top_nav.png";
import classifySubtitle from "@/assets/images/diy/product/classift_left_nav.png";

import { orderMealPage } from "@/components/DiyNew/diy/systemPages/orderMeal/dataConfig/config.js";

import fontControl from "@/components/DiyNew/diy/baseControl/fontControl/index.vue";
import colorControl from "@/components/DiyNew/diy/baseControl/colorControl/index.vue";
import uploadImg from "@/components/DiyNew/uploadImg/index.vue";
import chooseLink from "@/components/DiyNew/chooseLink/index.vue";

import productDefault from "@/assets/images/diy/product/product_full.png";
import productFlexTwo from "@/assets/images/diy/product/product_flex_two.png";
import productCoulmTwo from "@/assets/images/diy/product/product_coulm_two.png";
import productFlexThree from "@/assets/images/diy/product/product_flex_three.png";
// diy接口
import {
  getOrderMealBanners,
  editOrderMealBanners,
  deleteOrderMealBanners,
  getOrderMealStore,
  updateOrderMealStore,
} from "@/api/diy/diy";
import { getUrlParam } from "@/utils/utils.js";

// styleType => 1: 单标题 2: 双标题 3: 顶部导航 4: 左侧导航
export default {
  name: "orderMealPageControl",
  components: {
    fontControl,
    colorControl,
    uploadImg,
    chooseLink,
  },
  props: {
    params: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      exampleList: [
        {
          icon: classifySubtitle,
          type: "模板2",
          value: "1",
        },
        {
          icon: classifyDefault,
          type: "模板1",
          value: "2",
        },
      ],
      goodsExampleList: [
        {
          icon: productDefault,
          type: "模板1",
          value: "1",
        },
        {
          icon: productFlexTwo,
          type: "模板2",
          value: "2",
        },
        {
          icon: productCoulmTwo,
          type: "模板3",
          value: "3",
        },
        {
          icon: productFlexThree,
          type: "模板4",
          value: "4",
        },
      ],
      bannerList: [],
      showChooseLink: false,
      currentItem: {},
      currentIndex: -1,
      store_id: 1,
      logoImg: "",
      store: {},
    };
  },
  mounted() {
    if (getUrlParam("store_id")) this.store_id = getUrlParam("store_id");
    this.getBanners();
    this.getOrderMealStore();
  },
  watch: {},
  computed: {
    styleType: {
      get() {
        return this.params.params.styleType || "1";
      },
    },
    goodsType: {
      get() {
        return this.params.params.goodsType || "3";
      },
    },
  },
  methods: {
    getOrderMealStore() {
      getOrderMealStore().then((res) => {
        if (!res.code) {
          this.store = res.data;
          this.logoImg = res.data.logo;
        }
      });
    },
    getBanners() {
      getOrderMealBanners({ url: "/plugins/food/banner/list" }).then((res) => {
        if (!res.code) {
          this.bannerList = res.data.data;
        }
      });
    },
    // 重置文字设置
    resetFontCss() {
      let data = Object.assign({}, orderMealPage);
      this.$set(this.params.params, "fontCss", data.params.fontCss);
    },
    setLeftTabBg(e) {
      this.$set(this.params.params, "leftTabBg", e);
    },
    // 设置样式
    setStyleType(item) {
      this.$setWithAction(this.params, "styleType", item.value);
    },
      // 设置样式
    setGoodsStyleType(item) {
      this.$set(this.params.params, "goodsType", item.value);
    },
    setWithActionItem(e, key) {
      this.$setWithAction(this.params, key, e);
    },
    // 点击重置按钮
    resetItem(type) {
      let data = JSON.parse(JSON.stringify(orderMealPage));
      console.log(data);
      this.$set(this.params.params, type, data.params[type]);
    },
    // 图片上传成功回调
    uploadSuccess(e) {
      console.log(e);
      // 需要调用新建接口
      // this.bannerList.push({ pic_url: e.val });
      this.updateOrderMealBanners({
        page_url: "",
        pic_url: e.val,
        sort: 0,
        title: "",
      });
    },
    deleteImg(e) {
      // 调用删除接口
      console.log(e);
      deleteOrderMealBanners({ id: e.item.id }).then((res) => {
        if (!res.code) {
          this.getBanners();
          this.$eventHub.$emit("reloadOrderMealBanner");
        }
      });
    },
    // 点击单个图片
    handleImgItem(e) {
      let { item, index } = e;
      console.log(item, index);
      item.link = {
        link: item.page_url,
        name: "当前链接",
      };
      this.currentItem = item.link;
      this.currentIndex = index;
      this.showChooseLink = true;
    },
    // 更新图片
    changeImg(e) {
      console.log(e);
      let index = e.index;
      let list = this.bannerList;
      for (let i = 0; i < list.length; i++) {
        if (i == index) {
          list[i].pic_url = e.val;
          break;
        }
      }
      this.updateOrderMealBanners(list[index]);
    },
    // api更新banner
    updateOrderMealBanners(params) {
      editOrderMealBanners(params).then((res) => {
        console.log(res);
        if (!res.code) {
          this.getBanners();
          this.$eventHub.$emit("reloadOrderMealBanner");
        }
      });
    },
    // 选择链接
    chooseLink(e) {
      let index = this.currentIndex;
      let list = this.bannerList;
      for (let i = 0; i < list.length; i++) {
        if (i == index) {
          list[i].link = e;
          list[i].page_url = e.link;
          break;
        }
      }
      this.currentItem = list[index].link || {};
    },
    logoSuccess(e) {
      console.log(e);
      updateOrderMealStore({ logo: e.val }).then((res) => {
        if (!res.code) {
          this.logoImg = e.val;
          this.$eventHub.$emit("reloadOrderMealLogo");
        }
      });
      // 需要调用新建接口
    },
    deleteLogo() {
      this.logoImg = this.store.logo;
    },
  },
};
</script>

<style lang="scss" scoped>
.control-item {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  .control-header {
    width: 100%;
    height: 48px;
    background: #ffffff;
    border-bottom: 1px solid #f0f2f5;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px 14px 24px;
    span {
      font-size: 12px;
      color: #8c8c8c;
    }
    .control-name {
      font-size: 14px;
      font-weight: 600;
      color: #000000;
    }
    span:nth-child(2) {
      cursor: pointer;
    }
  }
  .control-body {
    width: 100%;
    padding-bottom: 20px;
    .control-section {
      width: 228px;
      margin: 0 auto;
      text-align: center;
      .control-item {
        width: 100%;
        .control-title {
          padding: 24px 0 14px 0;
          font-size: 12px;
          color: #000000;
          text-align: left;
        }
        .control-content {
          .example-list {
            width: 100%;
            display: flex;
            flex-wrap: wrap;
            .example-item {
              width: 70px;
              height: 70px;
              background: #f6f7f9;
              border-radius: 4px;
              margin-right: 9px;
              display: flex;
              justify-content: center;
              align-items: center;
              cursor: pointer;
              img {
                width: 70px;
                height: 70px;
              }
            }
            .example-item:last-child {
              margin-right: 0;
            }
            .active {
              border: 2px solid #1890fc;
            }
          }
          .goods-example-list {
            width: 100%;
            display: flex;
            flex-wrap: wrap;
            .example-item {
              width: 70px;
              height: 70px;
              background: #f6f7f9;
              border-radius: 4px;
              margin-right: 6px;
              margin-bottom: 8px;
              display: flex;
              justify-content: center;
              align-items: center;
              cursor: pointer;
              img {
                width: 70px;
                height: 70px;
              }
            }
            .example-item:last-child {
              margin-right: 0;
            }
            .active {
              border: 2px solid #1890fc;
            }
          }
        }
      }

      .classify-list {
        .control-content {
          .list-section {
            width: 228px;
            border: 2px solid #f6f7f9;
            border-radius: 4px;
            .list-box {
              width: 100%;
              max-height: 240px;
              background: #ffffff;
              overflow-x: hidden;
              overflow-y: scroll;
              .list-item {
                width: 100%;
                display: flex;
                height: 40px;
                justify-content: space-between;
                align-items: center;
                padding: 0 14px;
                .classify-name {
                  flex: 1;
                  text-align: left;
                }
                .icons-box {
                  display: flex;
                  align-items: center;
                  .sort-icons {
                    width: 24px;
                    height: 24px;
                    position: relative;
                    margin-right: 10px;
                    .icon-paixushang {
                      position: absolute;
                      left: 0;
                      top: 0;
                    }
                    .icon-paixuxia {
                      position: absolute;
                      right: 0;
                      top: 0;
                    }
                  }
                  .iconfont {
                    cursor: pointer;
                  }
                }
              }
              .active {
                background: #eaf5ff;
                border: 1px solid #1890fc;
              }
            }
            .add-classify-title {
              width: 100%;
              height: 40px;
              background: #ffffff;
              border-radius: 4px;
              display: flex;
              align-items: center;
              justify-content: center;
              cursor: pointer;
              .iconfont {
                font-size: 19px;
                color: #1890fc;
              }
              span {
                font-size: 14px;
                font-family: PingFang SC;
                font-weight: 600;
                color: #1890fc;
                margin-left: 4px;
              }
            }
            .add-border-top {
              border-top: 2px solid #f6f7f9;
            }
          }
        }
      }
      .flex-between {
        display: flex;
        justify-content: space-between;
        .reset {
          font-size: 12px;
          color: #9e9e9e;
          cursor: pointer;
        }
      }

      .classify-title {
        .control-content {
          .classify-title-input {
            width: 228px;
            height: 40px;
            background: #f6f7f9;
            border-radius: 4px;
            ::v-deep .ant-input {
              width: 100%;
              height: 40px;
              background: #f6f7f9;
              border: 0 none;
            }
          }
        }
      }
      .choose-product {
        .flex-between {
          display: flex;
          justify-content: space-between;
          .reset {
            font-size: 12px;
            color: #9e9e9e;
            cursor: pointer;
          }
        }
        .control-content {
          .data-tabs {
            width: 228px;
            height: 34px;
            background: #f6f7f9;
            border-radius: 4px;
            position: relative;
            .tab-bg {
              width: 113px;
              height: 30px;
              background: #ffffff;
              opacity: 1;
              border-radius: 4px;
              position: absolute;
              left: 2px;
              top: 2px;
              z-index: 11;
              transition: all 0.2s;
            }
            .active-select {
              left: 2px;
            }
            .active-classify {
              left: 113px;
            }
            .tab-content {
              width: 100%;
              height: 100%;
              position: absolute;
              left: 0;
              top: 0;
              z-index: 12;
              display: flex;
              justify-content: center;
              align-items: center;
              .tab-item {
                flex: 1;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 14px;
                font-weight: 400;
                color: #000000;
                opacity: 0.45;
                transition: all 0.2s;
                cursor: pointer;
                user-select: none;
              }
            }
          }
          .select-data-section {
            margin-top: 8px;
          }
          .select-data {
            width: 228px;
            height: 40px;
            background: #f6f7f9;
            border-radius: 4px;
            padding: 0 14px;
            display: flex;
            align-items: center;
            cursor: pointer;
            .iconfont {
              font-size: 18px;
            }
            span {
              font-size: 14px;
              font-family: PingFang SC;
              font-weight: 400;
              color: #000000;
              margin-left: 8px;
            }
          }
          .select-list {
            width: 224px;
            background: #ffffff;
            border: 2px solid #f6f7f9;
            max-height: 320px;
            overflow: auto;
            display: flex;
            flex-direction: column;
            align-items: center;
            box-sizing: content-box;
            .list-item {
              display: flex;
              align-items: center;
              padding: 0 14px;
              width: 100%;
              height: 64px;
              flex-shrink: 0;
              .pic-img {
                width: 44px;
                height: 44px;
                background: #ffffff;
                border-radius: 4px;
                margin-right: 6px;
                flex-shrink: 0;
              }
              .name {
                width: 98px;
                height: 20px;
                font-size: 14px;
                font-family: PingFang SC;
                font-weight: 400;
                color: #000000;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                margin-right: 10px;
                margin-bottom: 0;
              }
              .action {
                margin-bottom: 0;
                position: relative;
                width: 16px;
                height: 24px;
                .iconfont {
                  position: absolute;
                  top: 0;
                }
                .iconfont:nth-child(1) {
                  left: -4px;
                }
                .iconfont:nth-child(2) {
                  right: -4px;
                }
                .disabled {
                  color: #a6a6a6;
                }
              }
              .delete {
                font-size: 12px;
                margin-bottom: 0;
                margin-left: 10px;
              }
            }
          }
          .select-classify {
            width: 228px;
            height: 40px;
            background: #f6f7f9;
            border-radius: 4px;
            margin-top: 8px;
            ::v-deep .ant-select-selection {
              height: 40px;
              border: 0 none;
              background: #f6f7f9;
            }
            ::v-deep .ant-select-selection__rendered {
              line-height: 40px;
            }
          }
        }
      }
    }
  }
}
</style>
